package com.bsj.pay.util.platform.wsgw.sm;

import java.math.*;

public class SmUtil
{
    private static final char[] DIGITS_LOWER;
    private static final char[] DIGITS_UPPER;
    
    static {
        DIGITS_LOWER = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        DIGITS_UPPER = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    }
    
    public static String AsciiStringToString(final String s) {
        String string = "";
        for (int n = s.length() / 2, i = 0; i < n; ++i) {
            string += String.valueOf((char)hexStringToAlgorism(s.substring(i * 2, i * 2 + 2)));
        }
        return string;
    }
    
    public static String StringToAsciiString(final String s) {
        String string = "";
        for (int length = s.length(), i = 0; i < length; ++i) {
            string += Integer.toHexString(s.charAt(i));
        }
        return string;
    }
    
    public static String algorismToHEXString(final int n) {
        String s2;
        final String s = s2 = Integer.toHexString(n);
        if (s.length() % 2 == 1) {
            s2 = "0" + s;
        }
        return s2.toUpperCase();
    }
    
    public static String algorismToHexString(final int n, final int n2) {
        String s2;
        final String s = s2 = Integer.toHexString(n);
        if (s.length() % 2 == 1) {
            s2 = "0" + s;
        }
        return patchHexString(s2.toUpperCase(), n2);
    }
    
    public static int binaryToAlgorism(final String s) {
        final int length = s.length();
        int n = 0;
        for (int i = length; i > 0; --i) {
            n += (int)(Math.pow(2.0, length - i) * (s.charAt(i - 1) - '0'));
        }
        return n;
    }
    
    public static byte[] byteConvert32Bytes(final BigInteger bigInteger) {
        final byte[] array = null;
        final byte[] array2 = null;
        byte[] byteArray;
        if (bigInteger == null) {
            byteArray = array;
        }
        else if (bigInteger.toByteArray().length == 33) {
            final byte[] array3 = new byte[32];
            System.arraycopy(bigInteger.toByteArray(), 1, array3, 0, 32);
            byteArray = array3;
        }
        else if (bigInteger.toByteArray().length == 32) {
            byteArray = bigInteger.toByteArray();
        }
        else {
            final byte[] array4 = new byte[32];
            for (int i = 0; i < 32 - bigInteger.toByteArray().length; ++i) {
                array4[i] = 0;
            }
            System.arraycopy(bigInteger.toByteArray(), 0, array4, 32 - bigInteger.toByteArray().length, bigInteger.toByteArray().length);
            byteArray = array4;
        }
        return byteArray;
    }
    
    public static BigInteger byteConvertInteger(final byte[] array) {
        BigInteger bigInteger;
        if (array[0] < 0) {
            final byte[] array2 = new byte[array.length + 1];
            array2[0] = 0;
            System.arraycopy(array, 0, array2, 1, array.length);
            bigInteger = new BigInteger(array2);
        }
        else {
            bigInteger = new BigInteger(array);
        }
        return bigInteger;
    }
    
    public static String byteToHex(final byte[] array) {
        if (array == null) {
            throw new IllegalArgumentException("Argument b ( byte array ) is null! ");
        }
        String s = "";
        for (int i = 0; i < array.length; ++i) {
            final String hexString = Integer.toHexString(array[i] & 0xFF);
            if (hexString.length() == 1) {
                s = s + "0" + hexString;
            }
            else {
                s += hexString;
            }
        }
        return s.toUpperCase();
    }
    
    public static int byteToInt(final byte[] array) {
        return 0x0 | (array[0] & 0xFF) << 0 | (array[1] & 0xFF) << 8 | (array[2] & 0xFF) << 16 | (array[3] & 0xFF) << 24;
    }
    
    public static String byteToString(final byte[] array) {
        String string = "";
        for (int length = array.length, i = 0; i < length; ++i) {
            string += (char)array[i];
        }
        return string;
    }
    
    public static byte charToByte(final char c) {
        return (byte)"0123456789ABCDEF".indexOf(c);
    }
    
    public static byte[] decodeHex(final char[] array) {
        final int length = array.length;
        if ((length & 0x1) != 0x0) {
            throw new RuntimeException("Odd number of characters.");
        }
        final byte[] array2 = new byte[length >> 1];
        int digit;
        int digit2;
        for (int n = 0, i = 0; i < length; ++i, digit2 = toDigit(array[i], i), ++i, array2[n] = (byte)((digit << 4 | digit2) & 0xFF), ++n) {
            digit = toDigit(array[i], i);
        }
        return array2;
    }
    
    public static char[] encodeHex(final byte[] array) {
        return encodeHex(array, true);
    }
    
    public static char[] encodeHex(final byte[] array, final boolean b) {
        char[] array2;
        if (b) {
            array2 = SmUtil.DIGITS_LOWER;
        }
        else {
            array2 = SmUtil.DIGITS_UPPER;
        }
        return encodeHex(array, array2);
    }
    
    protected static char[] encodeHex(final byte[] array, final char[] array2) {
        final int length = array.length;
        final char[] array3 = new char[length << 1];
        int i = 0;
        int n = 0;
        while (i < length) {
            final int n2 = n + 1;
            array3[n] = array2[(array[i] & 0xF0) >>> 4];
            n = n2 + 1;
            array3[n2] = array2[array[i] & 0xF];
            ++i;
        }
        return array3;
    }
    
    public static String encodeHexString(final byte[] array) {
        return encodeHexString(array, true);
    }
    
    public static String encodeHexString(final byte[] array, final boolean b) {
        char[] array2;
        if (b) {
            array2 = SmUtil.DIGITS_LOWER;
        }
        else {
            array2 = SmUtil.DIGITS_UPPER;
        }
        return encodeHexString(array, array2);
    }
    
    protected static String encodeHexString(final byte[] array, final char[] array2) {
        return new String(encodeHex(array, array2));
    }
    
    public static String getHexString(final byte[] array) {
        return getHexString(array, true);
    }
    
    public static String getHexString(final byte[] array, final boolean b) {
        String string = "";
        for (int i = 0; i < array.length; ++i) {
            string += Integer.toString((array[i] & 0xFF) + 256, 16).substring(1);
        }
        String upperCase = string;
        if (b) {
            upperCase = string.toUpperCase();
        }
        return upperCase;
    }
    
    public static int hexStringToAlgorism(String upperCase) {
        upperCase = upperCase.toUpperCase();
        final int length = upperCase.length();
        int n = 0;
        for (int i = length; i > 0; --i) {
            int char1 = upperCase.charAt(i - 1);
            if (char1 >= 48 && char1 <= 57) {
                char1 -= 48;
            }
            else {
                char1 -= 55;
            }
            n += (int)(Math.pow(16.0, length - i) * char1);
        }
        return n;
    }
    
    public static String hexStringToBinary(String s) {
        final String upperCase = s.toUpperCase();
        String s2 = "";
        final int length = upperCase.length();
        int i = 0;
        while (i < length) {
            s = s2;
            Label_0138: {
                switch (upperCase.charAt(i)) {
                    default: {
                        s = s2;
                        break Label_0138;
                    }
                    case 'F': {
                        s = s2 + "1111";
                        break Label_0138;
                    }
                    case 'E': {
                        s = s2 + "1110";
                        break Label_0138;
                    }
                    case 'D': {
                        s = s2 + "1101";
                        break Label_0138;
                    }
                    case 'C': {
                        s = s2 + "1100";
                        break Label_0138;
                    }
                    case 'B': {
                        s = s2 + "1011";
                        break Label_0138;
                    }
                    case 'A': {
                        s = s2 + "1010";
                        break Label_0138;
                    }
                    case '9': {
                        s = s2 + "1001";
                        break Label_0138;
                    }
                    case '8': {
                        s = s2 + "1000";
                        break Label_0138;
                    }
                    case '7': {
                        s = s2 + "0111";
                        break Label_0138;
                    }
                    case '6': {
                        s = s2 + "0110";
                        break Label_0138;
                    }
                    case '5': {
                        s = s2 + "0101";
                        break Label_0138;
                    }
                    case '4': {
                        s = s2 + "0100";
                        break Label_0138;
                    }
                    case '3': {
                        s = s2 + "0011";
                        break Label_0138;
                    }
                    case '2': {
                        s = s2 + "0010";
                        break Label_0138;
                    }
                    case '1': {
                        s = s2 + "0001";
                        break Label_0138;
                    }
                    case '0': {
                        s = s2 + "0000";
                    }
                    case ':':
                    case ';':
                    case '<':
                    case '=':
                    case '>':
                    case '?':
                    case '@': {
                        ++i;
                        s2 = s;
                        continue;
                    }
                }
            }
        }
        return s2;
    }
    
    public static byte[] hexStringToBytes(String upperCase) {
        byte[] array2;
        if (upperCase != null && !upperCase.equals("")) {
            upperCase = upperCase.toUpperCase();
            final int n = upperCase.length() / 2;
            final char[] charArray = upperCase.toCharArray();
            final byte[] array = new byte[n];
            int n2 = 0;
            while (true) {
                array2 = array;
                if (n2 >= n) {
                    break;
                }
                final int n3 = n2 * 2;
                array[n2] = (byte)(charToByte(charArray[n3]) << 4 | charToByte(charArray[n3 + 1]));
                ++n2;
            }
        }
        else {
            array2 = null;
        }
        return array2;
    }
    
    public static String hexStringToString(final String s, final int n) {
        String string = "";
        for (int n2 = s.length() / n, i = 0; i < n2; ++i) {
            string += (char)hexStringToAlgorism(s.substring(i * n, (i + 1) * n));
        }
        return string;
    }
    
    public static byte[] hexToByte(final String s) throws IllegalArgumentException {
        if (s.length() % 2 != 0) {
            throw new IllegalArgumentException();
        }
        final char[] charArray = s.toCharArray();
        final byte[] array = new byte[s.length() / 2];
        int n = 0;
        int n2;
        for (int length = s.length(), i = 0; i < length; i = n2 + 1) {
            final StringBuilder append = new StringBuilder().append("");
            n2 = i + 1;
            array[n] = new Integer(Integer.parseInt(append.append(charArray[i]).append(charArray[n2]).toString(), 16) & 0xFF).byteValue();
            ++n;
        }
        return array;
    }
    
    public static byte[] intToBytes(final int n) {
        return new byte[] { (byte)(n >> 0 & 0xFF), (byte)(n >> 8 & 0xFF), (byte)(n >> 16 & 0xFF), (byte)(n >> 24 & 0xFF) };
    }
    
    public static byte[] longToBytes(final long n) {
        final byte[] array = new byte[8];
        for (int i = 0; i < 8; ++i) {
            array[i] = (byte)(0xFFL & n >> i * 8);
        }
        return array;
    }
    
    public static int parseToInt(final String s, int int1) {
        try {
            int1 = Integer.parseInt(s);
            return int1;
        }
        catch (NumberFormatException ex) {
            return int1;
        }
    }
    
    public static int parseToInt(final String s, int int1, int n) {
        try {
            n = (int1 = Integer.parseInt(s, n));
            return int1;
        }
        catch (NumberFormatException ex) {
            return int1;
        }
    }
    
    public static String patchHexString(final String s, final int n) {
        String string = "";
        for (int i = 0; i < n - s.length(); ++i) {
            string = "0" + string;
        }
        return (string + s).substring(0, n);
    }
    
    public static void printHexString(final byte[] array) {
        for (int i = 0; i < array.length; ++i) {
            String s2;
            final String s = s2 = Integer.toHexString(array[i] & 0xFF);
            if (s.length() == 1) {
                s2 = '0' + s;
            }
            System.out.print("0x" + s2.toUpperCase() + ",");
        }
        System.out.println("");
    }
    
    public static byte[] subByte(final byte[] array, final int n, final int n2) {
        final byte[] array2 = new byte[n2];
        for (int i = 0; i < n2; ++i) {
            array2[i] = array[i + n];
        }
        return array2;
    }
    
    protected static int toDigit(final char c, final int n) {
        final int digit = Character.digit(c, 16);
        if (digit == -1) {
            throw new RuntimeException("Illegal hexadecimal character " + c + " at index " + n);
        }
        return digit;
    }
}
